import { MagicWeaponBase } from "./MagicWeaponBase";
import { GImageBitmap } from "../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../resource/ImagesConst";
import { ObjCreate } from "../common/ObjCreate";
import { ScrollPolicy } from "../../../../fl/controls/ScrollPolicy";
import { ResolveItemRenderer } from "./renderer/ResolveItemRenderer";
import { ListEvent } from "../../../../fl/events/ListEvent";
import { BgItem } from "../common/BgItem";
import { Language } from "../../../../extend/language/Language";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { MagicWeaponItemData } from "./MagicWeaponItemData";
import { MsgManager } from "../../manager/MsgManager";
import { DelAlertWindow } from "./DelAlertWindow";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { Cache } from "../../cache/Cache";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { GTileList } from "../../../../com/mui/controls/GTileList";
import { GButton } from "../../../../com/mui/controls/GButton";
import { GLabel } from "../../../../com/mui/controls/GLabel";
import { FireTip } from "../common/item/FireTip";
type int = number;
//class MagicWeaponResolvePanel
    
    export  class MagicWeaponResolvePanel extends MagicWeaponBase
    {
        constructor()
        {
            super();
            panelType = MagicWeaponBase.RESOLVE_PANEL;
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            var loc1=new GImageBitmap(ImagesConst.MagicWeaponBg, 43, 188);
            this.addChild(loc1);
            this._operateMaterialList = ObjCreate.createTileList(30, 178, 420, 260, this);
            this._operateMaterialList.scrollPolicy = ScrollPolicy.OFF;
            this._operateMaterialList.columnWidth = 78;
            this._operateMaterialList.rowHeight = 85;
            this._operateMaterialList.setStyle("cellRenderer", ResolveItemRenderer);
            this._operateMaterialList.addEventListener(ListEvent.ITEM_CLICK, this.onCancelItemHandler);
            operateMaterialList = this._operateMaterialList;
            this._productBg = new Object /* flash.display.Bitmap */();
            ObjCreate.setObjAttri(this._productBg, 123, 350, -1, -1, this);
            this._noBindStone = new BgItem();
            this._noBindStone.x = 171;
            this._noBindStone.y = 358;
            this.addChild(this._noBindStone);
            this._bindStone = new BgItem();
            this._bindStone.x = 238;
            this._bindStone.y = 358;
            this.addChild(this._bindStone);
            this._onekeyAddBtn = ObjCreate.createGButton(Language.getString(27659), 120, 412, 80, 28, this, "ButtonRed");
            this._onekeyAddBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOnekeyAddHandler);
            this._resolveBtn = ObjCreate.createGButton(Language.getString(27661), 240, 412, 90, 28, this, "ButtonRed");
            this._resolveBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onResolveHandler);
            ObjCreate.createBg(31, 452, 380, 2, this, ImagesConst.SplitLine);
            this._fireTip = ObjCreate.createFireTip(Language.getString(27685), 30, 455, this, true);
            this.operateMaterialData = [];
            var loc2=ObjCreate.createImgBtn(ImagesConst.RuleExplain, 350, 460, 60, 23, this, "Button");
            loc2.gbutton.toolTipData = Language.getString(27677);
            loc2.gbutton.addEventListener(flash.events.MouseEvent.CLICK, this.clickHelpHandler);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.clean);
            LoaderHelp.addResCallBack(ResFileConst.magicWeapon, this.onLoadResHandler);
            return;
        }

        /* internal  */clickHelpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponShowPageGuide));
            return;
        }

        /* internal  */onLoadResHandler(): void
        {
            this._productBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.ResolveBg);
            return;
        }

        public clean(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=_materialData;
            for(loc1 of loc3) 
            {
                if (!(loc1 && loc1.isSelect)) 
                {
                    continue;
                }
                loc1.isSelect = false;
            }
            this.operateMaterialData = [];
            _noBindStoneNum = 0;
            _bindStoneNum = 0;
            this.updataResolveProduct(_noBindStoneNum, _bindStoneNum);
            if (arg1 && pageSelecter) 
            {
                restPage();
            }
            return;
        }

        /* internal  */onResolveHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var i: int;
            var arr: Array<any>;
            var len: int;
            var itemData: MagicWeaponItemData;
            var hasMoreThanFire: Boolean;
            var onClickHandler: Function;

            var loc1;
            i = 0;
            arr = null;
            itemData = null;
            hasMoreThanFire = false;
            e = arg1;
            arr = [];
            len = this._operateMaterialList.dataProvider.length;
            i = 0;
            while (i < len) 
            {
                itemData = this._operateMaterialList.dataProvider.getItemAt(i) as MagicWeaponItemData;
                if (itemData && itemData.itemData && itemData.itemData.uid) 
                {
                    if (itemData.itemData.magicWeaponLv >= 5) 
                    {
                        hasMoreThanFire = true;
                    }
                    arr.push(itemData.itemData.uid);
                }
                ++i;
            }
            if (arr.length <= 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(27682));
                return;
            }
            if (hasMoreThanFire) 
            {
                onClickHandler = (): void
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponResolve, {"items": arr}));
                    return;
                }
                DelAlertWindow.instance.showWindow(Language.getString(27689), Language.getString(27690), onClickHandler);
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponResolve, {"items": arr}));
            return;
        }

        /* internal  */onOnekeyAddHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            _noBindStoneNum = 0;
            _bindStoneNum = 0;
            this.clean();
            this.operateMaterialData = _materialData;
            this.updateWeaponInfo();
            return;
        }

        /* internal  */onCancelItemHandler(arg1: ListEvent): void
        {
            var loc2=0;
            var loc3=null;
            var loc1=arg1.item as MagicWeaponItemData;
            if (loc1 && loc1.itemData) 
            {
                loc2 = this._operateMaterialList.dataProvider.getItemIndex(loc1);
                this._operateMaterialList.dataProvider.replaceItemAt(new MagicWeaponItemData(null), loc2);
                this._operateMaterialList.drawNow();
                var loc4=0;
                var loc5=_materialData;
                for(loc3 of loc5) 
                {
                    if (loc3.itemData.uid != loc1.itemData.uid) 
                    {
                        continue;
                    }
                    loc3.isSelect = false;
                    if (ItemsUtil.isBind(loc3.itemData)) 
                    {
                        var loc6;
                        _bindStoneNum--;
                    }
                    else 
                    {
                        _noBindStoneNum--;
                    }
                    break;
                }
                this.updateWeaponInfo();
                this.updataResolveProduct(_noBindStoneNum, _bindStoneNum);
            }
            return;
        }

        /* internal  */updataResolveProduct(arg1: int, arg2: int): void
        {
            if (arg1 != 0) 
            {
                this._noBindStone.setItemCode(14143300, arg1);
            }
            else 
            {
                this._noBindStone.setItemCode(0, arg1);
            }
            if (arg2 != 0) 
            {
                this._bindStone.setItemCode(14143301, arg2);
            }
            else 
            {
                this._bindStone.setItemCode(0, arg2);
            }
            return;
        }

        /* internal  */get isSelectItemsUid(): string
        {
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc1="";
            if (this._operateMaterialList) 
            {
                loc3 = this._operateMaterialList.dataProvider.length;
                loc4 = 0;
                while (loc4 < loc3) 
                {
                    loc2 = this._operateMaterialList.dataProvider.getItemAt(loc4) as MagicWeaponItemData;
                    if (loc2 && loc2.isSelect && loc2.itemData) 
                    {
                        loc1 = loc1 + ("-" + loc2.itemData.uid);
                    }
                    ++loc4;
                }
            }
            return loc1;
        }

        public resolveSucceed(): void
        {
            this.operateMaterialData = [];
            _noBindStoneNum = 0;
            _bindStoneNum = 0;
            this.updataResolveProduct(_noBindStoneNum, _bindStoneNum);
            return;
        }

        public updateMoney(): void
        {
            if (this._goldLabel && this._coinLabel) 
            {
                this._goldLabel.text = String(Cache.instance.role.money.gold);
                this._coinLabel.text = String(Cache.instance.role.money.coin);
            }
            return;
        }

        public /* override */ updateWeaponInfo(): void
        {
            var loc3=null;
            var loc1=Cache.instance.magicWeapon.hasIdentifyAndNoWear;
            var loc2=this.isSelectItemsUid;
            var loc4=0;
            var loc5=loc1;
            for(loc3 of loc5) 
            {
                if (loc2.indexOf(loc3.itemData.uid) == -1) 
                {
                    continue;
                }
                loc3.isSelect = true;
            }
            _materialList.dataProvider = new DataProvider(loc1);
            _materialList.drawNow();
            _materialData = loc1;
            if (pageSelecter) 
            {
                pageSelecter.maxPage = int(Math.ceil(loc1.length / 6));
                currentPage();
            }
            return;
        }

        protected /* override */ set operateMaterialData(arg1: Array<any>)
        {
            super.operateMaterialData = arg1;
            this.updataResolveProduct(_noBindStoneNum, _bindStoneNum);
            return;
        }

        protected /* override */ onSelectItemHandler(arg1: ListEvent): void
        {
            var loc1=arg1.item as MagicWeaponItemData;
            if (loc1) 
            {
                if (loc1.isSelect) 
                {
                    return;
                }
                super.onSelectItemHandler(arg1);
                this.updateWeaponInfo();
                this.updataResolveProduct(_noBindStoneNum, _bindStoneNum);
            }
            return;
        }

        /* internal */ /* var */_operateMaterialList: GTileList;

        /* internal */ /* var */_onekeyAddBtn: GButton;

        /* internal */ /* var */_resolveBtn: GButton;

        /* internal */ /* var */_goldLabel: GLabel;

        /* internal */ /* var */_coinLabel: GLabel;

        /* internal */ /* var */_noBindStone: BgItem;

        /* internal */ /* var */_bindStone: BgItem;

        /* internal */ /* var */_fireTip: FireTip;

        /* internal */ /* var */_productBg: Object /* flash.display.Bitmap */;
    }
